package com.cci.kangdao.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.MailBoxTDao;
import com.cci.kangdao.dao.UserTDao;
import com.cci.kangdao.dao.model.MailBoxT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.service.MailBoxService;
import com.cci.kangdao.service.PropertiesUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.NotifyUtil;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/10/11
 */
@Service
public class MailBoxServiceImpl implements MailBoxService {

    @Resource
    private MailBoxTDao mailBoxTDao;
    @Resource
    private UserTDao userTDao;
    @Autowired
    private PropertiesUtils propertiesUtils;

    @Override
    public void getMessageList(Map<String, Object> map) {
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        // 分页信息
        Integer defaultPageCount = Integer.valueOf(this.propertiesUtils.getPropertiesValue("pagesize"));
        Integer pageNo = MapUtils.getInteger(map, "pageNo", 1);
        Integer pageCount = MapUtils.getInteger(map, "pageCount", defaultPageCount);
        // 默认全站公告
        Integer category = MapUtils.getInteger(map, "category", 1);
        // 消息ID
        Long messageId = MapUtils.getLong(map, "messageId");
        // 用户信息
        UserT userT = UserThreadLocal.get();

        // 消息列表
        List<Map<String, Object>> recordList = new ArrayList<>();
        Long totalRecord = 0L;
        // 如果传输消息ID，则为获取详情，反之，获取分页列表
        if (NumberUtils.isPositiveNum(messageId)) {
            totalRecord = 1L;
            MailBoxT mailBoxT = mailBoxTDao.selectByPrimaryKey(messageId);
            if (null != mailBoxT) {
                recordList.add(getResponseMessageMap(mailBoxT));
            }
        } else {
            // 拼装查询条件，进行分页查询
            PageHelper.startPage(pageNo, pageCount, true);
            MailBoxT queryMailBox = new MailBoxT();
            queryMailBox.setCategory(category);
            queryMailBox.setReceiverId(userT.getId());
            List<MailBoxT> mailBoxTList = mailBoxTDao.getMessageListByCondition(queryMailBox);
            PageInfo pageInfo = new PageInfo<>(mailBoxTList);
            //解析查询结果
            totalRecord = pageInfo.getTotal();
            if (CollectionUtils.isNotEmpty(pageInfo.getList())) {
                pageInfo.getList().forEach(o -> {
                    MailBoxT mailBoxT = (MailBoxT) o;
                    //转换前端响应参数
                    recordList.add(getResponseMessageMap(mailBoxT));
                });
            }
            mailBoxTDao.updateIsReadByReceiverId(userT.getId(),userT.getId(),LocalTimeUtil.getRealCurrentTime());
        }
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("totalRecord", totalRecord);
        responseMap.put("recordList", recordList);
        ParameterTool.writeSuccessResponse(response, responseMap);
    }
    private Map<String, Object> getResponseMessageMap(MailBoxT mailBoxT) {
        Map<String, Object> messageMap = new HashMap<>();
        messageMap.put("id", mailBoxT.getId());
        messageMap.put("category", mailBoxT.getCategory());
        messageMap.put("title", mailBoxT.getTitle());
        messageMap.put("content", mailBoxT.getContent());
        messageMap.put("isRead", mailBoxT.getIsRead());
        messageMap.put("createTime", mailBoxT.getCreateTime());
        return messageMap;
    }

    @Override
    @Transactional
    public void pushMessage(Map<String, Object> map) {
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        // 用户信息
        UserT currentUser = UserThreadLocal.get();
        // 默认全站公告
        Integer category = MapUtils.getInteger(map, "category", 1);
        String title = MapUtils.getString(map, "title");
        String content = MapUtils.getString(map, "content");
        // 全站公告逻辑
        if (category.equals(1)) {
            String currentTime = LocalTimeUtil.getRealCurrentTime();
            // 保存所有人员的发送记录
            MailBoxT mailBoxT = new MailBoxT();
            mailBoxT.setCategory(category);
            mailBoxT.setTitle(title);
            mailBoxT.setContent(content);
            mailBoxT.setIsRead(0);
            mailBoxT.setFlag(0);
            mailBoxT.setCreator(currentUser.getId());
            mailBoxT.setCreateTime(currentTime);
            mailBoxTDao.insertBatchBySql(mailBoxT);
            // 发送APP全部推送
            NotifyUtil.pushMessageToApp(title, 2, title);
        }
        ParameterTool.writeSuccessResponse(response);
    }


}
